package com.byzoro.service;

import com.byzoro.mapper.UsersMapper;
import com.byzoro.pojo.Users;
import com.byzoro.utils.JsonResult;
import com.byzoro.utils.Page;
import com.byzoro.utils.RestfulMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;

@Service
public class UsersService {

    @Autowired
    UsersMapper usersMapper;
    public JsonResult login (String name, String password) {
        Users user = usersMapper.getUserByName(name);
        if (user == null) {
            return JsonResult.fail("用户不存在");
        }
        if (!user.getPassword().equals(password)) {
            return JsonResult.fail("密码不正确！");
        }
        //更新最后一次登录时间
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        int row = usersMapper.updateLastLoginTimeByName(name, dateFormat.format(calendar.getTime()));
        if (row > 0) {
            return JsonResult.success("登录成功", user);
        }
        return JsonResult.fail("登录失败");
    }

    public JsonResult list (int page, int limit) {
        if (page <= 0) page = 1;
        if (limit <= 1) limit = 1;
        int offset = Page.page(page, limit);
        List<Users> user = usersMapper.list(offset, limit);
        int count = usersMapper.count();
        return JsonResult.list("获取成功", user, count);
    }

    public JsonResult create (Users users) {
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        users.setCreatedAt(dateFormat.format(calendar.getTime()));
        users.setLastLoginTime(null);
        users.setSort(1);//默认为1

        //判断用户是否存在
        Users u = usersMapper.getUserByName(users.getName());
        if (null != u) {
            return JsonResult.fail("用户名已存在");
        }
        int row = usersMapper.create(users);
        if (row > 0) {
            return JsonResult.success(RestfulMessage.OpOk);
        } else {
            return JsonResult.fail(RestfulMessage.OpFail);
        }
    }

    public JsonResult update (Users users) {
        //判断用户是否存在
        Users u = usersMapper.getUserById(users.getId());
        if (null == u) {
            return JsonResult.fail("用户已不存在");
        }
        int row = usersMapper.update(users);
        if (row >= 0) {
            return JsonResult.success(RestfulMessage.OpOk);
        } else {
            return JsonResult.fail(RestfulMessage.OpFail);
        }
    }

    public JsonResult delete (int id) {
        Users u = usersMapper.getUserById(id);
        if (null == u) {
            return JsonResult.fail("用户已不存在，请刷新页面重试");
        }
        if (Objects.equals(u.getName(), "admin")) {
            return JsonResult.fail("超级管理员不能删除");
        }
        int row = usersMapper.delete(id);
        if (row > 0) {
            return JsonResult.success(RestfulMessage.OpOk);
        } else {
            return JsonResult.fail(RestfulMessage.OpFail);
        }
    }

    public JsonResult getUserById (int id) {
        Users user = usersMapper.getUserById(id);
        return JsonResult.success(RestfulMessage.GetOk, user);
    }
}
